package com.ereconquista.web;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.ereconquista.dao.RubroDAO;
import com.ereconquista.modelo.Rubro;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Claudio
 */
@ManagedBean 
@SessionScoped
public class RubroBean implements Serializable{

    private RubroDAO rubroDAO;
    private List<Rubro> rubros;
    private List<Rubro> subRubros;
    


    /** Creates a new instance of LocalidadBean */
    public RubroBean() {

    }

    @PostConstruct
    private void init(){
        rubroDAO = new RubroDAO();
        rubros = rubroDAO.getListaByNivel(1, true);
    }

    public void filtrarLocalidades(Rubro r){
        
        subRubros = r.getRubros();
    }    

    //-------------------------------------------------------------------------

   
    
    public Converter getConverterRubro(){
        
        return new Converter() {

            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {
                if (value.trim().equals("") || value == null) {
                    return null;
                }
                int id = Integer.parseInt(value);
                Rubro r = rubroDAO.getRubro(id);
                return r;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {
                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Rubro) value).getId().toString() + "";
                }
            }
        };
    }

    public List<Rubro> getRubros() {
        return rubros;
    }

    public void setRubros(List<Rubro> rubros) {
        this.rubros = rubros;
    }

    public List<Rubro> getSubRubros() {
        return subRubros;
    }

    public void setSubRubros(List<Rubro> subRubros) {
        this.subRubros = subRubros;
    }

    


    
}
